package com.example.demo.controller;

import jakarta.servlet.http.*;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RequestMapping("/session")
@RestController
public class SessionController {
    @RequestMapping("/getC")
    public String getCookie(HttpServletRequest request, HttpServletResponse response){
        //获取参数
        String name = request.getParameter("name");
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            Arrays.stream(cookies).forEach(ck -> System.out.println(ck.getName()+":"+ck.getValue()));
        }
        return "获取cookie";

    }
    @RequestMapping("/getC2")
    public String getCookie2(@CookieValue("bite") String bite){
        //获取参数
        return "从cookie中获取值，bite："+bite;
    }
    @RequestMapping("/setSess")
    public String setSess(HttpServletRequest request){
        //从cookie中获取了sessionID，根据sessionID获取session对象，如果没有获取到，会创建一个session对象
        HttpSession session = request.getSession();
        session.setAttribute("name","zhangsan");
        return "设置session成功";
    }
    @RequestMapping("/getSess")
    public String getSess(HttpServletRequest request){
        //从cookie中获取了sessionID，根据session获取session对象
        HttpSession session = request.getSession();
        String name = (String)session.getAttribute("name");
        return "从session中获取name: "+name;
    }
    //封装
    @RequestMapping("/getSess2")
    public String getSess2(HttpSession session){
        //从cookie中获取了sessionID，根据session获取session对象
        String name = (String)session.getAttribute("name");
        return "从session中获取name: "+name;
    }
    @RequestMapping("/getSess3")
    public String getSess3(@SessionAttribute("name") String name){
        //从cookie中获取了sessionID，根据session获取session对象
        return "从session中获取name: "+name;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取信息： "+userAgent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){

        return "从header中获取信息： "+userAgent;
    }


}
